﻿using System;
using System.Collections.Generic;
using System.Drawing;
using DevComponents.DotNetBar;
using WoSoft.PACS.Win.MyToolBars;
using DevComponents.DotNetBar.Controls;
using WoSoft.PACS.Data.BaseData;
using WoSoft.PACS.Data.DepartmentDoctor;
using System.Windows.Forms;
using System.IO;
using System.Threading;

namespace WoSoft.PACS.Win
{
    /// <summary>
    /// 所有子窗口基类
    /// </summary>
    public partial class FrmBase : DevComponents.DotNetBar.OfficeForm
    {
        private RibbonBar _toolbar;

        //按钮的可用性
        private Dictionary<string, bool> _buttonEanleds = new Dictionary<string, bool>();

        public FrmBase()
        {
            InitializeComponent();
            this.Icon = global::WoSoft.PACS.Win.Properties.Resources.main;
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
        }

        public FrmBase(RibbonBar toolbar)
        {
            InitializeComponent();
            _toolbar = toolbar;
            this.Icon = global::WoSoft.PACS.Win.Properties.Resources.main;
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider();
        }

        //加载窗口
        protected override void OnLoad(EventArgs e)
        {
            //加载操作
            //LoadOperations();
            base.OnLoad(e);
        }

        //加载操作
        public void LoadOperations()
        {
            if (_toolbar != null)
            {
                _toolbar.Items.Clear();
                //_toolbar.Update();
                _toolbar.Refresh();
            }

            var opts = this.Operations();
            if (opts == null || opts.Count <= 0)
            {
                return;
            }

            foreach (OperationModel button in opts)
            {
                ButtonItem baseItem = null;

                baseItem = new ButtonItem(button.ID, button.Name);
                baseItem.ButtonStyle = eButtonStyle.ImageAndText;
                baseItem.Image = button.Image;
                baseItem.ImagePosition = eImagePosition.Top;
                baseItem.Enabled = button.Enabled;

                if (_buttonEanleds.ContainsKey(button.ID))
                {
                    //设置按钮可用性
                    baseItem.Enabled = _buttonEanleds[button.ID];
                }

                if (button.ClickEvent != null)
                {
                    baseItem.Click += button.ClickEvent;
                }

                if (string.IsNullOrEmpty(button.GroupName))
                {
                    button.GroupName = this.Text;
                }

                var ribbonBar = new RibbonBar();
                ribbonBar.Text = button.GroupName;
                ribbonBar.AutoOverflowEnabled = false;
                ribbonBar.BackgroundMouseOverStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
                ribbonBar.BackgroundStyle.CornerType = DevComponents.DotNetBar.eCornerType.Square;
                ribbonBar.ResizeItemsToFit = false;
                ribbonBar.Style = DevComponents.DotNetBar.eDotNetBarStyle.StyleManagerControlled;
                _toolbar.Items.Add(baseItem);
            }

            _toolbar.Update();
            _toolbar.Size = new Size(1000, 80);
        }

        //关闭窗口
        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            if (this._toolbar != null)
            {
                this._toolbar.Items.Clear();
            }
        }

        /// <summary>
        /// 该窗体所有操作方法
        /// </summary>
        public virtual List<OperationModel> Operations()
        {
            return null;
        }

        //设置工具栏的可用性
        protected void EnabledToolbarItem(string buttonID, bool isEnabled)
        {
            if (_toolbar != null)
            {
                var item = _toolbar.GetItem(buttonID);
                if (item != null)
                {
                    if (!_buttonEanleds.ContainsKey(buttonID))
                    {
                        //添加按钮可用性集合中
                        _buttonEanleds.Add(buttonID, isEnabled);
                    }
                    else
                    {
                        _buttonEanleds[buttonID] = isEnabled;
                    }
                    item.Enabled = isEnabled;
                    item.UpdateBindings();
                    _toolbar.Update();
                }
            }
        }

        #region 公共控件数据加载
        //加载民族数据源
        protected void LoadNationList(ComboBoxEx combox)
        {
            var nationData = new NationData();
            var dt = nationData.GetAllData();

            combox.DataSource = dt;
            combox.DisplayMember = "名称";
            combox.ValueMember = "编码";
        }

        //加载职业
        protected void LoadWorker(ComboBoxEx combox)
        {
            var worker = new SystemCode();
            var dt = worker.GetWorkerList();
            combox.DataSource = dt;
            combox.DisplayMember = "名称";
            combox.ValueMember = "编码";
        }

        //加载性别数据源
        //0-未知;1-男;2-女;9-未说明的性别
        protected void LoadSexList(ComboBoxEx cbSex)
        {
            LoadCodeList(1, cbSex);
        }

        //加载婚姻
        protected void LoadMarriageList(ComboBoxEx cbe)
        {
            LoadCodeList(4, cbe);
        }

        //加载学历
        protected void LoadEducationList(ComboBoxEx cbe)
        {
            LoadCodeList(3, cbe);
        }

        //加载保险类型
        protected void LoadPayModeList(ComboBoxEx cbSex)
        {
            LoadCodeList(5, cbSex);
        }

        //加载户口类型
        protected void LoadAccountList(ComboBoxEx cbAccountType)
        {
            LoadCodeList(6, cbAccountType);
        }

        //加载编码
        private void LoadCodeList(int cid, ComboBoxEx cbox)
        {
            var baseCode = new SystemCode();
            var dt = baseCode.GetCodeList(cid);
            cbox.DataSource = dt;
            cbox.DisplayMember = "名称";
            cbox.ValueMember = "编码";
        }

        //加载科室数据
        protected void LoadDepartment(ComboTree cbox)
        {
            var department = new DepartmentDoctorData();
            var dt = department.GetAllList();
            cbox.DataSource = dt;
            cbox.DisplayMembers = "简称";
            cbox.ValueMember = "ID";
            cbox.ParentFieldNames = "ID,上级ID";
            cbox.AdvTree.ExpandAll();
            cbox.SelectedIndex = -1;
        }

        //加载操作员
        protected void LoadUser(ComboBoxEx cbox)
        {
            cbox.Text = string.Empty;
            var department = new DepartmentDoctorData();
            var dt = department.GetUserAllList();
            cbox.DataSource = dt;
            cbox.DisplayMember = "用户名称";
            cbox.ValueMember = "ID";    
        }

        //加载医生数据
        protected void LoadDoctorToCombox(string departmentid, ComboBoxEx cbox)
        {
            cbox.Text = string.Empty;
            var department = new DepartmentDoctorData();
            var dt = department.GetDataByDepartmentID(departmentid);
            cbox.DataSource = dt;
            cbox.DisplayMember = "姓名";
            cbox.ValueMember = "ID";
        }
        #endregion

        #region 生成拼音首字母
        protected void SetSpellCodeControl(Control tb1, TextBoxX tb2)
        {
            TextBoxX inputControl = tb1 as TextBoxX;
            if (inputControl != null)
            {
                inputControl.Tag = tb2;
                inputControl.Validating += new System.ComponentModel.CancelEventHandler(inputControl_Validating);
            }
            else
            {
                TextBoxDropDown tbdd = tb1 as TextBoxDropDown;
                if (tbdd!=null)
                {
                    tbdd.Tag = tb2;
                    tbdd.Validating += new System.ComponentModel.CancelEventHandler(inputControl_Validating);
                }
            }
        }

        //控件录入验证
        void inputControl_Validating(object sender, System.ComponentModel.CancelEventArgs e)
        {
            TextBoxX tb = sender as TextBoxX;
            if (tb != null)
            {
                var tb2 = tb.Tag as TextBoxX;
                if (tb2 != null)
                {
                    var code = PACS.Utility.SpellCode.SpellCodeHelper.GetFirstPYLetter(tb.Text.Trim());
                    tb2.Text = code;
                }
            }
            else
            {
                var tbdd = sender as TextBoxDropDown;
                if (tbdd!=null)
                {
                    var tb2 = tbdd.Tag as TextBoxX;
                    if (tb2 != null)
                    {
                        var code = PACS.Utility.SpellCode.SpellCodeHelper.GetFirstPYLetter(tbdd.Text.Trim());
                        tb2.Text = code;
                    }
                }
            }
        }

        #endregion

        protected byte[] FileToByte(string filePath)
        {
            string tempPath = Application.StartupPath + @"\Temp\" + Path.GetFileName(filePath);
            if (!File.Exists(tempPath))
            {
                //File.Copy(filePath, tempPath);
                FileInfo fileinfo = new FileInfo(filePath);
                fileinfo.CopyTo(tempPath);
                Thread.Sleep(300);
            }
            FileStream fs;
            using (fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
            {
                BinaryReader br = new BinaryReader(fs);
                byte[] bytes = br.ReadBytes((int)fs.Length);

                return bytes;
            }

        }


        #region 图像与二进制数组转换
        protected byte[] ImageToByte(Bitmap img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] bytes = ms.GetBuffer();  //byte[]   bytes=   ms.ToArray(); 这两句都可以，至于区别么，下面有解释
            ms.Close();
            return bytes;
        }

        protected Bitmap ByteToImage(byte[] data)
        {
            byte[] bytelist = data;
            MemoryStream ms1 = new MemoryStream(bytelist);
            Bitmap bm = (Bitmap)Image.FromStream(ms1);
            ms1.Close();

            return bm;
        }
        #endregion
    }
}
